# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pybind11
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.4.3
pkgrel=1
pkgdesc="A lightweight header-only library that exposes C++ types in Python and vice versa (mingw-w64)"
url='https://pybind11.readthedocs.org/'
license=('BSD')
arch=('any')
optdepends=("${MINGW_PACKAGE_PREFIX}-python3: to target bindings supporting python 3"
            "${MINGW_PACKAGE_PREFIX}-python2: to target bindings supporting python 2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-breathe"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/pybind/pybind11/archive/v${pkgver}.tar.gz")
sha256sums=('1eed57bc6863190e35637290f97a20c81cfe4d9090ac0a24f3bbf08f265eb71d')

prepare() {
  cd ${srcdir}
  cp -r ${_realname}-${pkgver} build-python2
  cp -r ${_realname}-${pkgver} build-python3
}

build () {
  printf '%s\n' '  -> Building Python3 modules...'
  cd "${srcdir}/build-python3"
  ${MINGW_PREFIX}/bin/python3 setup.py build
  
  printf '%s\n' '  -> Building Python2 modules...'
  cd "${srcdir}/build-python2"
  ${MINGW_PREFIX}/bin/python2 setup.py build
  
  printf '%s\n' '  -> Building cmake files...'
  cd "${srcdir}/build-python3/build"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python3 \
    ..
  make mock_install
  
  printf '%s\n' '  -> Building manpage...'
  cd "${srcdir}/build-python3/docs"
  make man
}

package() {
  local _pythonver
  _pythonver="$(${MINGW_PREFIX}/bin/python3 --version | awk '{ print $2 }' | grep -o '^[0-9]*\.[0-9]*')"
  
  # python3 modules
  cd "${srcdir}/build-python3"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install_lib --install-dir="${pkgdir}${MINGW_PREFIX}/lib/python${_pythonver}/site-packages" --optimize='1'
  
  # --prefix=${MINGW_PREFIX} --root="${pkgdir}"
  # python2 modules
  cd "${srcdir}/build-python2"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install_lib --install-dir="${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages" --optimize='1'
  
  # C++ headers
  cd "${srcdir}/build-python3"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install_headers --install-dir="${pkgdir}${MINGW_PREFIX}/include/${_realname}"
  
  # cmake files
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  cd build/mock_install/share/cmake/pybind11
  install -m644 *.cmake "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  
  # manpage
  cd "${srcdir}/build-python3/docs/.build/man"
  install -D -m644 "${_realname}.1" -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  
  # license
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
